package com.beiing.tianshuai.tianshuai.huodong.model;

import android.util.Log;

import com.beiing.tianshuai.tianshuai.entity.EventTypesBean;
import com.beiing.tianshuai.tianshuai.entity.VersionInfoBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * 获取活动的类型
 * Created by magikliu on 2017/7/21.
 */

public class HuoDongModel implements HuoDongModelImpl {

    private static final String TAG = "HuoDongModel";
    private OnRequestListener mListener;

    public HuoDongModel(OnRequestListener mListener) {
        this.mListener = mListener;
    }

    @Override
    public void getTabTitles() {
        DisposableObserver<EventTypesBean> observer = new DisposableObserver<EventTypesBean>() {
            @Override
            public void onNext(@NonNull EventTypesBean huoDongType) {
                Log.i(TAG, huoDongType.getData().getNumber().get(1).getName().toString());
                mListener.onSuccess(huoDongType);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                mListener.onError(e);
            }

            @Override
            public void onComplete() {

            }
        };
        DataManager.getEventTypes("10")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    @Override
    public void getVersionInfo() {
        Observable<VersionInfoBean> response =
                DataManager.getVersionInfo();
        Observable<VersionInfoBean> observable = response.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observable.subscribe(new DisposableObserver<VersionInfoBean>() {
            @Override
            public void onNext(@NonNull VersionInfoBean bean) {
                if (mListener != null) {
                    mListener.onRequestVersionInfoSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("===================== onComplete =======================", TAG);
            }
        });
    }

    public interface OnRequestListener{
        void onRequestVersionInfoSuccess(VersionInfoBean bean);
        void onSuccess(EventTypesBean bean);
        void onError(Throwable throwable);
    }
}
